package com.ki.rent.controller;

import com.ki.rent.entity.Style;
import com.ki.rent.entity.User;
import com.ki.rent.service.StyleService;
import com.ki.rent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class AddStyleController {

    @Autowired
    private StyleService styleService;
    @Autowired
    private UserService userService;


    private String dirPath = "D:/apacheProject/Work1/src/main/resources/static/style/";

    // 详细图片地址
    private StringBuilder detailsPath = new StringBuilder();


    @RequestMapping("/MultipleUploads")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("file") List<MultipartFile> file, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String,Object>();
        if (!file.isEmpty() && file.size() > 0) {
            for (MultipartFile f : file) {
                try {
                    // 文件名
                    String filename = UUID.randomUUID()
                            + f.getOriginalFilename().substring(f.getOriginalFilename().lastIndexOf("."));
                    // 存储虚拟路径
                    String localPath = filename+"~";
                    System.out.println(localPath);
                    detailsPath.append(localPath);
                    File filePath = new File(dirPath);
                    if (!filePath.exists()) {
                        filePath.mkdirs();
                    }
                    //上传
                    f.transferTo(new File(dirPath + filename));

                } catch (Exception e) {
                    map.put("code", 1);
                    map.put("msg", "上传失败");
                    e.printStackTrace();
                }
            }
            map.put("code", 0);
            map.put("msg", "上传成功");
        }
        return map;
    }

    @RequestMapping("/addStyle")
    public String addHouse(Model model, HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        if (userId !=null){
            User user = userService.getById(userId);
            model.addAttribute("user",user);
            return "style/addStyle";
        }
        else {
            User user = userService.getById(0);
            model.addAttribute("user",user);
            return "house/addStyle";
        }
    }

    @RequestMapping("/addStyleRecord")
    @ResponseBody
    public String addHouse(Style style) {

        style.setStyleImg(detailsPath.toString());
        boolean n = styleService.saveOrUpdate(style);
        if(n) {
            detailsPath.delete(0,detailsPath.length());
            return "OK";
        }
        return "FAIL";
    }
}
